package ldkxingzhe.ldkxingzhedemo.location;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import java.util.List;

import ldkxingzhe.ldkxingzhedemo.R;

/**
 * Created by ke on 16-2-1.
 */
public class LocationTestActivity extends AppCompatActivity
        implements View.OnClickListener, LocationListener {
    @SuppressWarnings("unused")
    private static final String TAG = "LocationTestActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_test);
        findViewById(R.id.button).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        List<String> availableProviders = manager.getProviders(true);
        Log.v(TAG, "provider's size is " + availableProviders.size());
        for (String str : availableProviders) {
            Log.v(TAG, "str is " + str);
        }
        Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if(location == null) {
            Criteria criteria = new Criteria();
            criteria.setCostAllowed(false);
            criteria.setBearingRequired(false);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
            List<String> list = manager.getProviders(criteria, true);
            Log.v(TAG, "now list.size is " + list.size());
            manager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, this, Looper.getMainLooper());
//            manager.requestSingleUpdate(criteria, this, Looper.myLooper());
            return;
        }
        Log.v(TAG, "location is " + location.getLongitude() + "," + location.getLatitude());
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.v(TAG, "location is " + location.getLongitude() + "," + location.getLatitude());
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
        Log.v(TAG, "onStatusChanged()");
    }

    @Override
    public void onProviderEnabled(String s) {
        Log.v(TAG, "onProviderEnabled");
    }

    @Override
    public void onProviderDisabled(String s) {
        Log.v(TAG, "onProviderDisabled");
    }
}
